package com.huike.web.controller.report;


import com.huike.common.core.domain.entity.BCStatisticsVo;
import com.huike.common.utils.SecurityUtils;
import com.huike.report.domain.vo.IndexTodayInfoVO;
import com.huike.report.domain.vo.IndexTodoInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.huike.common.core.domain.AjaxResult;
import com.huike.report.service.IReportService;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import static java.time.LocalDate.now;

@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private IReportService reportService;


    /**
     * 首页--基础数据统计
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @GetMapping("/getBaseInfo")
    public AjaxResult getBaseInfo(@RequestParam("beginCreateTime") String beginCreateTime,
                                  @RequestParam("endCreateTime") String endCreateTime) {
        return AjaxResult.success(reportService.getBaseInfo(beginCreateTime, endCreateTime));
    }

    /**
     * 获取首页基础数据信息
     */
    @GetMapping("/getTodayInfo")
    public AjaxResult getTodayInfo() {
        //    当前用户  在  当天的
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String username = SecurityUtils.getUsername();

        //    线索数量
        CompletableFuture<Integer> todayCluesNum = CompletableFuture.supplyAsync(() -> reportService.getTodayCluesNum(username, now));

        //    商机数量
        CompletableFuture<Integer> todayBusinessNum = CompletableFuture.supplyAsync(() -> reportService.getTodayBusinessNum(username, now));

        //    合同数量
        CompletableFuture<Integer> todayContractNum = CompletableFuture.supplyAsync(() -> reportService.getTodayContractNum(username, now));

        //    成交的金额
        CompletableFuture<Double> todaySalesAmount = CompletableFuture.supplyAsync(() -> reportService.getTodaySalesAmount(username, now));

        //所有线程结束后再执行下面方法
        CompletableFuture.allOf(todayCluesNum,todayBusinessNum,todayContractNum,todaySalesAmount).join();

        //封装数据提交
        IndexTodayInfoVO infoVO = new IndexTodayInfoVO();
        try {
            infoVO.setTodayCluesNum(todayCluesNum.get());
            infoVO.setTodayBusinessNum(todayBusinessNum.get());
            infoVO.setTodayContractNum(todayContractNum.get());
            infoVO.setTodaySalesAmount(todaySalesAmount.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return AjaxResult.success(infoVO);

    }
    /**
     * 商机转化龙虎榜接口
     */
    @GetMapping("/businessChangeStatistics")
    public AjaxResult businessChangeStatistics(@RequestParam("beginCreateTime") String beginCreateTime,
                                                @RequestParam("endCreateTime") String endCreateTime){
        //商机和合同两张表具有不合理处，无法区分同名成员

        //哪些部门员工将商机转合同的最多
        //1.在tb_contract合同表中，筛选出create_time在指定时间内的所有数据，以员工id进行分组，累加所有id
        List<BCStatisticsVo> list=reportService.businessChangeStatistics(beginCreateTime,endCreateTime);

        //2.通过list获取的数据，遍历得到员工的部门id，通过部门id找到部门名称
        for (BCStatisticsVo vo : list) {
            Integer deptId = vo.getDeptId();
            String deptName=reportService.getDeptName(deptId);
            vo.setDeptName(deptName);
            //3.计算转化率
            //无法确定用作基数的合同到底指的是什么，正常来说需要通过员工id来找到对应的商机然后按照时间筛选防止重名
            //但是此时两张表中都没有员工id，所以只能通过姓名来查，但是姓名的create_by无法确定能否对应商机表的create_by
            //因为商机表的create_by是创建人的名字，创建人不等于将商机转合同的人，所以无法搜索。
            //在tb_contract合同表中。筛选出create_time在指定时间内的所有create_by，且tb_business商机表中
            //

        }

        //3.计算转化率
        //在tb_business表中。筛选出create_time在指定时间内的所有数据，

        return AjaxResult.success(list);
    }
    /**
     * 待办数据统计接口
     */
    @GetMapping("/getTodoInfo")
    public AjaxResult getTodoInfo(String beginCreateTime,String endCreateTime){
        //待跟进线索数目指线索表中status为1的数据数量
        CompletableFuture<Integer> tofollowedCluesNum = CompletableFuture.supplyAsync(() -> reportService.getTofollowedCluesNum(beginCreateTime, endCreateTime));

        //待跟进商机数目指商机表中status为1的数据数量
        CompletableFuture<Integer> tofollowedBusinessNum = CompletableFuture.supplyAsync(() -> reportService.getTofollowedBusinessNum(beginCreateTime, endCreateTime));

        //待分配线索数目指分配记录表中type为0的数据数量
        CompletableFuture<Integer> toallocatedCluesNum = CompletableFuture.supplyAsync(() -> reportService.getToallocatedCluesNum(beginCreateTime, endCreateTime));

        //待分配商机数目指分配记录表中type为1的数据数量
        CompletableFuture<Integer> toallocatedBusinessNum = CompletableFuture.supplyAsync(() -> reportService.getToallocatedBusinessNum(beginCreateTime, endCreateTime));

        CompletableFuture.allOf(tofollowedCluesNum,tofollowedBusinessNum,toallocatedCluesNum,toallocatedBusinessNum);
        IndexTodoInfoVO infoVO = new IndexTodoInfoVO();
        try {
            //待跟进线索数目
            infoVO.setTofollowedCluesNum(tofollowedCluesNum.get());
            //待跟进商机数目
            infoVO.setTofollowedBusinessNum(tofollowedBusinessNum.get());
            //待分配线索数目
            infoVO.setToallocatedCluesNum(toallocatedCluesNum.get());
            //待分配商机数目
            infoVO.setToallocatedBusinessNum(toallocatedBusinessNum.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return AjaxResult.success(infoVO);
    }

}